package com.ruoyi.spider.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.oss.cloud.CloudConstant;
import com.ruoyi.oss.cloud.CloudStorageConfig;
import com.ruoyi.oss.cloud.CloudStorageService;
import com.ruoyi.oss.cloud.OSSFactory;
import com.ruoyi.oss.domain.OssException;
import com.ruoyi.oss.domain.SysOss;
import com.ruoyi.oss.service.ISysOssService;
import com.ruoyi.spider.domain.SpiderArticle;
import com.ruoyi.spider.service.ISpiderArticleService;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 爬虫文章Controller
 * 
 * @author 小光头
 * @date 2021-01-13
 */
@Controller
@RequestMapping("/spider/spiderArticle")
public class SpiderArticleController extends BaseController
{
    private String prefix = "spider/spiderArticle";

    @Autowired
    private ISpiderArticleService spiderArticleService;
    @Autowired
    private ISysOssService sysOssService;

    @Autowired
    private ISysConfigService sysConfigService;
    @RequiresPermissions("spider:spiderArticle:view")
    @GetMapping()
    public String spiderArticle()
    {
        return prefix + "/spiderArticle";
    }

    /**
     * 查询爬虫文章列表
     */
    @RequiresPermissions("spider:spiderArticle:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SpiderArticle spiderArticle)
    {
        startPage();
        List<SpiderArticle> list = spiderArticleService.selectSpiderArticleList(spiderArticle);
        return getDataTable(list);
    }

    /**
     * 导出爬虫文章列表
     */
    @RequiresPermissions("spider:spiderArticle:export")
    @Log(title = "爬虫文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SpiderArticle spiderArticle)
    {
        List<SpiderArticle> list = spiderArticleService.selectSpiderArticleList(spiderArticle);
        ExcelUtil<SpiderArticle> util = new ExcelUtil<SpiderArticle>(SpiderArticle.class);
        return util.exportExcel(list, "spiderArticle");
    }

    /**
     * 新增爬虫文章
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存爬虫文章
     */
    @RequiresPermissions("spider:spiderArticle:add")
    @Log(title = "爬虫文章", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SpiderArticle spiderArticle)
    {
        return toAjax(spiderArticleService.insertSpiderArticle(spiderArticle));
    }

    /**
     * 修改爬虫文章
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        SpiderArticle spiderArticle = spiderArticleService.selectSpiderArticleById(id);
        mmap.put("spiderArticle", spiderArticle);

        return prefix + "/edit";
    }

    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    @RequiresPermissions("spider:oss:add")
    @ResponseBody
    public AjaxResult upload(@RequestParam("file") MultipartFile file) throws Exception
    {
        if (file.isEmpty())
        {
            throw new OssException("上传文件不能为空");
        }
        // 上传文件
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        CloudStorageService storage = OSSFactory.build();
        // 获取云存储配置信息
        String jsonconfig = sysConfigService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY);

        CloudStorageConfig config = JSON.parseObject(jsonconfig, CloudStorageConfig.class);

        //拼接后缀
        String url = storage.uploadSuffix(file.getBytes(), suffix)+ "/" + config.getQiniuBucketImgStyleSuffix();


        // 保存文件信息
        SysOss ossEntity = new SysOss();
        ossEntity.setUrl(url);
        ossEntity.setFileSuffix(suffix);
        ossEntity.setCreateBy(ShiroUtils.getLoginName());
        ossEntity.setFileName(fileName);
        ossEntity.setCreateTime(new Date());
        ossEntity.setService(storage.getService());
        return toAjax(sysOssService.save(ossEntity)).put("url", ossEntity.getUrl()).put("fileName",ossEntity.getFileName());
    }



    /**
     * 修改保存爬虫文章
     */
    @RequiresPermissions("spider:spiderArticle:edit")
    @Log(title = "爬虫文章", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SpiderArticle spiderArticle)
    {
        return toAjax(spiderArticleService.updateSpiderArticle(spiderArticle));
    }

    /**
     * 删除爬虫文章
     */
    @RequiresPermissions("spider:spiderArticle:remove")
    @Log(title = "爬虫文章", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(spiderArticleService.deleteSpiderArticleByIds(ids));
    }
}
